<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!--

    
 
  Copyright  2009  Sun Microsystems, Inc. All rights reserved.

-->
</head>
<body bgcolor="white">

Dispatches Java and native events to Java subsystems. See midpEvents.h for
the native interface to the MIDP event system.

<blockquote><p align="center"><img src="doc-files/events_1.gif" border=0></p>
</blockquote>

<h4>Adding a New Event Type to the MIDP Java Event System</h4>
<ol>
<li>
First select the new event type ID to be next the highest one in midpEvents.h.
 Add the new event type ID to midpEvents.h</li>
<li>
If the event is a native event and there a not enough fields in
 NativeEvent, add the required fields to both NativeEvent and MidpEvent
 midpEvents.h. Follow the generic naming scheme.</li>
<li>
Implement an EventListener.</li>

<blockquote><pre>
class MyListener implements EventListener {
    public boolean preprocess(Event event, Event waitingEvent) {
        // only one of these events should be in the queue at a time

        if (waitingEvent == null) {
            // let the event be put in the queue
            return true;
        }

        ... merge the new event into the waiting event ...

        // signal that this event should not be put in the queue
        return false;
    }

    public void process(Event genericEvent) {
        MyEvent myEvent = (MyEvent)genericEvent;

        ... process the event ...
    }
}
</pre></blockquote>

<li>
Add code to obtain a security token for the class that is going to register
 the event listenter or post an Java event. The code depends on if the
 class is optional or not. If the class to receive the security token is
 optional then the class must implement the <code>ImplictlyTrustedClass</code>
 interface and have a public no argument constructor and so an instance of
 the class can be loaded by name in the
 <code>initializeInternalSecurity</code> method of the
 <code>MIDletSuiteLoader</code> and be given a token. If the class normal
 part of MIDP it can just
 implement a static method that sets the classes security token if the token
 has not already been set and insert a call to this static method in
 <code>initializeInternalSecurity</code>.</li>
</ol>
<h4>Previous and Currently Implemented Thread Models</h4>
The first MIDP event queue had one thread which blocked until a native
 event was available and then processed the event in the same thread and
 looped back to the blocking event read. If events were only generated by
 native code this would be fine but for Java generated events like repaint,
 this means that three native methods had to be called, one to put the
 Java event in the native event queue and two to retrieve it from the
 native event queue.
<p>
To speed up Java to Java events like repaints a Java level queue was
 introduced to avoid the any native method calls. Processing was in one
 thread and native methods were feed into the Java queue by another thread
 to avoid polling the native event queue. This boosted the repaint rate
 three fold.</p>
<p>
To speed up native event response during continuous event activity,
 the thread switch that occurred to process a native event after it
 was read was eliminated.
 This was done by letting the Java queue processing thread read events in
 a non-blocking way after a it had processed the pending Java events.
 To avoid polling when the processing thread ran out of events to process
 the queue would sleep. Just before sleeping, the Java queue processing
 thread wakes up a separate thread that monitors the native event queue in
 a blocking fashion, wakes up the Java queue processing thread when a native
 event was available to be read and goes to sleep. This model is used by
 the real device implementation and will be used for Leap Frog.</p>
</body>
</html>
